package de.lulusoft.anothertorrent.core.rtorrent;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;


/**
 * Represents a rTorrent View.
 * @author gordi
 *
 */
public class View implements PropertyChangeListener{

	private TorrentArrayList torrents = null;
	protected TorrentArrayList updatedTorrents = null;
	protected TorrentArrayList newTorrents = null;
	protected TorrentArrayList removedTorrents = null;
	private String name = "";
	
	public static final String PROP_NAME = "NameProperty";
	public static final String PROP_NEW = "NewTorrentProperty";
	public static final String PROP_UPDATED = "UpdatedTorrentProperty";
	public static final String PROP_REMOVED = "RemovedTorrentProperty";
	
	private PropertyChangeSupport propertyChangeSupport = null;
	
	public View(String name) {
		super();
		this.name = name;
		this.torrents = new TorrentArrayList();
		this.propertyChangeSupport = new PropertyChangeSupport(this);
		this.updatedTorrents = new TorrentArrayList();
		this.newTorrents = new TorrentArrayList();
		this.removedTorrents = new TorrentArrayList();
	}
	
	public void addTorrent(Torrent torrent)
	{
		torrent.addPropertyChangeListener(this);
		torrents.add(torrent);
	}
	
	public void removeTorrent(Torrent torrent)
	{
		torrent.removePropertyChangeListener(this);
		torrents.remove(torrent);
	}

	public TorrentArrayList getTorrents() {
		return torrents;
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		String oldValue = this.name;
		this.name = name;
		propertyChangeSupport.firePropertyChange(PROP_NAME, oldValue, name);
	}
	
//	/**
//	 * Updates the Views Torrents.
//	 */
//	public void updateModel()
//	{
//		try {
//			RTorrentManager.updateView(this);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//				
//	}
	
	/**
	 * Notifies all Listeners about the updates of a
	 * previous updateModel call.
	 */
	public void updateGUI()
	{		
		try {
			if (updatedTorrents.size() > 0)
				propertyChangeSupport.firePropertyChange(PROP_UPDATED, null, updatedTorrents);
			if (newTorrents.size() > 0)
				propertyChangeSupport.firePropertyChange(PROP_NEW, null, newTorrents);
			if (removedTorrents.size() > 0)
				propertyChangeSupport.firePropertyChange(PROP_REMOVED, null, removedTorrents);
			updatedTorrents.clear();
			newTorrents.clear();
			removedTorrents.clear();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}	
	
	public void addListener(PropertyChangeListener listener)
	{
		propertyChangeSupport.addPropertyChangeListener(listener);
	}
	
	public void removeListener(PropertyChangeListener listener)
	{
		propertyChangeSupport.removePropertyChangeListener(listener);
	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		if (evt.getPropertyName().equals(Torrent.PROP_CHANGED))
		{
			if (evt.getSource() instanceof Torrent)
			{
				updatedTorrents.add((Torrent)evt.getSource());
				propertyChangeSupport.firePropertyChange(PROP_UPDATED, null, updatedTorrents);
			}
		}
	}
	

}
